package exchange

import (
	"banzhuan/models"
	"banzhuan/rpc"
	"encoding/json"
	"github.com/shopspring/decimal"
	"log"
)

func GetDepth(symbol string,tickDenom int,method string) (string,int,int){
	body:=rpc.HttpGet("pub","depth",symbol)
	//log.Println(string(body))
	depth:=new(models.Depth)
	err:=json.Unmarshal(body,&depth)
	if err!=nil {
		//log.Println("GetDepth",err)
	}
	//log.Println(symbol,"lots:",depth.Asks[0].Lots,"sell ticks:",depth.Asks[0].Ticks)
	//log.Println(symbol,"lots:",depth.Bids[0].Lots,"buy_0 ticks:",depth.Bids[0].Ticks)
	defer func() {
		//恢复程序的控制权
		err := recover()
		if err != nil {
			//log.Println("err：",err)
		}
	}()
	if depth.Bids[0].Ticks!=0 && depth.Asks[0].Ticks!=0 {
		tickDenom_0:=decimal.NewFromFloat(IntToFloat64(tickDenom))
		sell_0:=decimal.NewFromFloat(IntToFloat64(depth.Asks[0].Ticks))
		sell_0_count:=depth.Asks[0].Lots
		buy_0:=decimal.NewFromFloat(IntToFloat64(depth.Bids[0].Ticks))
		buy_0_count:=depth.Bids[0].Lots
		sell:=sell_0.Div(tickDenom_0)
		buy:=buy_0.Div(tickDenom_0)
		//log.Println("sell:",sell)
		//log.Println("buy:",buy)
		if method=="SELL" && sell_0.String()!="0"{
			return sell.String(),depth.Asks[0].Ticks,sell_0_count
		}else if method=="BUY" && buy_0.String()!="0" {
			return buy.String(),depth.Bids[0].Ticks,buy_0_count
		}
		log.Println("error===================symbol:",symbol,"tickDenom:",tickDenom,"method:",method)
		return "error",-1,-1
	}
	return "error",-1,-1
}
